//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//

#include "DisplayProperties.hpp"
#include "ioctl.h"
#include "regmgr.h"
#include "propnames.h"

static	const	int	s_MAX_DISPLAYVALUELEN = 255;

CDisplayPropPage::CDisplayPropPage (CTerminalPropSheet *pOwner) : 
	CTerminalPropPage(pOwner), 
	m_bScrSaver(FALSE), 
	m_nScrTimeout(10), 
	m_displayType(0), 
	m_curSetting(0),
	m_bTested (FALSE),
	m_colorChanged(TRUE),
	headColorInfo(NULL),
	m_bDisplayBbar(TRUE)
	
{
	m_displayChanged=FALSE;
	m_bWizard = pOwner->GetWizardFlag();
	DEBUGMSG (1, (L"CDisplayPropPage::CDisplayPropPage() \n"));
	Init (IDD_DISPLAY_PAGE);
	OEMDisplayInit();

	memset(&m_displayTableName,0,sizeof(void *) * MAX_DISPLAY_KEYS);
}
	
CDisplayPropPage::~CDisplayPropPage()
{
	PCOLORINFO tempColorInfo = NULL;
    
	if ( oemDisplay )
	{
		delete oemDisplay;
	}

	while (headColorInfo)
	{
		tempColorInfo = headColorInfo;
		headColorInfo = headColorInfo->next;
		free (tempColorInfo);
	}
	if( m_displayTableName )
	{
		for (int i=0; i < MAX_DISPLAY_KEYS; i++)
		{
			if (m_displayTableName[i] != NULL)
			{
				delete[] m_displayTableName[i];
				m_displayTableName[i] = NULL;
			}
		}
	}
}


LRESULT CDisplayPropPage::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	DEBUGMSG (1, (L"CDisplayPropPage::OnInitDialog() -- class is [%s]\n", GetId()))	;
	CGenericPropPage::OnInitDialog (uMsg, wParam, lParam, bHandled);

	// attach controls
	if(!m_bWizard)
	{
		m_scrSaverCheck.Attach (GetDlgItem(IDC_ENABLE_SCR_SVR_CHK));
		m_scrSaverTimeout.Attach (GetDlgItem(IDC_SPIN_EDIT));
		m_spinctl.Attach (GetDlgItem(IDC_SPIN1));
		m_scrWait.Attach(GetDlgItem(IDC_WAIT_STATIC));
		// set initial values
        m_spinctl.SendMessage (UDM_SETRANGE, 0, MAKELPARAM(20,1));
        m_spinctl.SendMessage (UDM_SETPOS, 0, MAKELONG(20, 0));
	

		UpdateScreenSaver();	
	}
	else if (!m_pWbtWiz)
	{
		DEBUGMSG (1, (L"WBTWIZ::CDisplayPropPage::OnInitDialog() -- !! Error -  couldn't get owner !!\n"));
		EndDialog (0);
		return 0;
	}

	m_displayChoices.Attach (GetDlgItem(IDC_DISPLAY_COMBO));

	m_test.Attach (GetDlgItem(IDC_TEST_BTN));
	//disabling in the beginning
	m_test.EnableWindow(FALSE);

	if (m_bWizard) 
	{
		m_colorChoices.Attach(GetDlgItem(IDC_COLOR_COMBO));
		m_wndBitmap.Attach(GetDlgItem(IDC_COLOR_IMAGE));
	}

	DEBUGMSG(1,(L"ATLPROP::OnInitDialog() -- Calling Color Choices"));

	if (!UpdateColorChoices())
	{
		DEBUGMSG(1,(L"ATLPROP::OnInitDialog() -- Failed to Initialize Colors"));
		return -1;
	}
	if (!UpdateDisplayChoices())
	{
		RETAILMSG(1,(L"ATLPROP::OnInitDialog() -- Failed to Initialize display"));
		return -1;
	}
	
	// update the bitmap display
	UpdateBitmap();

	return 1;
}


LRESULT CDisplayPropPage::OnCommandMsg(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	
	
	if ( HIWORD(wParam) == CBN_SELCHANGE )
		OnDisplayChoicesChange();
	else if ( !m_bWizard &&( HIWORD(wParam) == EN_UPDATE ) && ( LOWORD(wParam) == IDC_SPIN_EDIT ) 
			&& (m_scrSaverTimeout != NULL))
	{
		TCHAR szBuf[10]={0};
		int timeOut = 0;
	
		m_scrSaverTimeout.GetWindowText(szBuf, sizeof(szBuf) / sizeof(szBuf[0]));
		
		if ( ( szBuf != NULL ) && ( szBuf[0] != '\0' ) )
		{
			timeOut = _ttoi (szBuf);
			if( ( timeOut <= 0 ) || ( timeOut > (DEFAULT_SCREENSAVER_TIMEOUT/60))) 
			{
				LPCTSTR pszTitle = LoadStringEx (IDS_TIMEOUT_CAPTION);
				LPCTSTR pszMsg   = LoadStringEx (IDS_TIMEOUT_ERROR);

				if (pszTitle && pszMsg)
					MessageBox (pszMsg, pszTitle, MB_ICONINFORMATION | MB_OK);

				m_scrSaverTimeout.SetWindowText(L"");
			}
		}
		else if ( wcscmp(szBuf,L"")==0)
		{
			m_scrSaverTimeout.SetWindowText(L"1");
			DEBUGMSG(1,(L"ATLPROP::OnInitDialog() -- Scr Saver Timeout is SET to 1"));
			m_scrSaverTimeout.SetFocus();
			return -1;
		}

	}
	return 1;
}


LRESULT CDisplayPropPage::OnScrCheck (WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	int state  = Button_GetCheck (m_scrSaverCheck);
	m_bScrSaver = ((state) == BST_CHECKED);
	DEBUGMSG (1, (L"ATLPROP::OnScrCheck() -- m_bScrSaver = %d\n", m_bScrSaver));
	//MyTrace ((L"m_bScrSaver = %d\n", m_bScrSaver));
	m_scrSaverTimeout.EnableWindow (m_bScrSaver);
	m_scrWait.EnableWindow (m_bScrSaver);
	
	return 1;
}

LRESULT CDisplayPropPage::OnBbarCheck (WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	int state  = Button_GetCheck (m_wndDisplayBbar);
	m_bDisplayBbar = ((state) == BST_CHECKED);
	return 1;
}

//
// On Color Resolution change load appropriate BITMAP.
// 
void CDisplayPropPage::UpdateBitmap()
{
	HBITMAP hBmp = NULL;
	int index=0, bmpIndx=0;
	TCHAR szSelColor[COLORCOMBO_LEN]={0};	

	PCOLORINFO tempColorInfo = NULL;
	
	if (!m_bWizard) 
		return;

	index = m_colorChoices.SendMessage( CB_GETCURSEL,0,0 );	
	
	if ((index == CB_ERR) || (m_colorChoices.SendMessage ( CB_GETLBTEXT,
		(WPARAM)index,
		(LPARAM)szSelColor) 
		== CB_ERR ))
	{
		RETAILMSG (1,(L"CDisplayPropPage::UpdateBitmap( )--CB_GETLBTEXT failed"));
		return;
	}
	
	for( tempColorInfo = headColorInfo; tempColorInfo; tempColorInfo=tempColorInfo->next)
	{
		if (wcscmp(tempColorInfo->colorTableName,szSelColor)==0)
		{
			break;
		}
		bmpIndx++;
	}
	
	switch (bmpIndx)
	{
		case Color8:
			hBmp = LoadBitmap( ghInstance, MAKEINTRESOURCE (IDB_COLOR8));
			break;
		case Color16:
			hBmp = LoadBitmap( ghInstance, MAKEINTRESOURCE (IDB_COLOR16));
			break;
		default:
			hBmp = LoadBitmap( ghInstance, MAKEINTRESOURCE (IDB_COLOR24));
	}
	m_wndBitmap.SendMessage (STM_SETIMAGE, IMAGE_BITMAP, (LPARAM) hBmp);

}

LRESULT CDisplayPropPage::OnColorChoicesChange (WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	UpdateBitmap();
	return 1;
}

void CDisplayPropPage::UpdateScreenSaver()
{
	Button_SetCheck (m_scrSaverCheck, m_bScrSaver ? BST_CHECKED : BST_UNCHECKED);
	SetDlgItemInt (m_hwnd, IDC_SPIN_EDIT, m_nScrTimeout, TRUE);
	m_scrSaverTimeout.EnableWindow (m_bScrSaver);
	m_scrWait.EnableWindow (m_bScrSaver);
}

BOOL CDisplayPropPage::ReadRegistrySettings()
{
	if(!m_bWizard)
	{
		m_bScrSaver = (GetScreenSaverEnable() == 1);
		m_nScrTimeout = GetScreenSaverTimeout() / 60; // convert seconds -> minutes

		m_bDisplayBbar = GetDisplayBbar();
	}
	return TRUE;
}

BOOL CDisplayPropPage::WriteRegistrySettings()
{
	RETAILMSG(1,(L"CDisplayPropPage::WriteRegistrySettings():: Writing in to the registry"));
	if(m_displayChanged)
	{
		if ( SetDisplayInfo() == FALSE )
		{
			// Error recovery and divert the user to safe place.
			RETAILMSG(1,(L"CDisplayPropPage::WriteRegistrySettings() -- Display Information Could Not be saved") );
			
		}
		else
		{
			RETAILMSG(1,(L"CDisplayPropPage::WriteRegistrySettings() -- Display Information Is saved") );
		}
	}

	if(!m_bWizard)
	{
		if (!m_bScrSaver)
			SetScreenSaverEnable (0);
		else
		{
			SetScreenSaverEnable (1);
			SetScreenSaverTimeout((DWORD)m_nScrTimeout * 60); // convert minutes -> seconds
			SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, (DWORD)m_nScrTimeout * 60, NULL, TRUE);
		}

		SetDisplayBbar(m_bDisplayBbar);
	}

	if ( m_colorChanged )
	{
		if ( SetColorInfo() == FALSE )
		{
			// Error recovery and divert the user to safe place.
			RETAILMSG(1,(L"CDisplayPropPage::WriteRegistrySettings() -- Color Information Could Not be saved") );
			
		}
		else
		{
			RETAILMSG(1,(L"CDisplayPropPage::WriteRegistrySettings() -- Color Information Is saved") );
		}

	}
	
	return TRUE;
}

//
// set the color info in the registry
//
BOOL CDisplayPropPage::SetColorInfo( )
{
	UINT selIndex = 0;
	UINT colIndex = 0;
	HKEY hKey = NULL;
	TCHAR szSelColor[COLORCOMBO_LEN]={0};
	PCOLORINFO tempColorInfo = NULL;

	if (!m_bWizard) 
		return TRUE;

	selIndex = m_colorChoices.SendMessage( CB_GETCURSEL,0,0 );
	
	if ((selIndex == CB_ERR) || (m_colorChoices.SendMessage ( CB_GETLBTEXT,
		(WPARAM)selIndex,
		(LPARAM)szSelColor) 
		== CB_ERR ))
	{
		RETAILMSG (1,(L"CDisplayPropPage::SetColorInfo( )--CB_GETLBTEXT failed"));
		return FALSE;
	}
	
	hKey = OpenRegistryKey (HKLM, WBT_MSTSC_SETTINGS, KEY_READ);
	if ( hKey  == NULL )
	{
		// Create the Color Key
		hKey = CreateRegistryKey(HKLM, WBT_MSTSC_SETTINGS);
		if ( hKey == NULL )
		{
			RETAILMSG(1,(L"CDisplayPropPage::SetColorInfo() -- Registry key Creation failed"));
			return FALSE;			
		}
	}
	
	for( tempColorInfo = headColorInfo; tempColorInfo; tempColorInfo=tempColorInfo->next)
	{
		if (wcscmp(tempColorInfo->colorTableName,szSelColor)==0)
		{
			break;
		}
	}
	// set the color value corresponding to the selection in the combobox.
	
	if (tempColorInfo)
	{
		SetRegistryKeyDWORD( hKey, COLOR_SETTINGS, tempColorInfo->colorTableValue);
		RETAILMSG (1,(L"CDisplayPropPage::SetColorInfo()--The Value Being Stored is: %d",
			tempColorInfo->colorTableValue));

		//AAS
		OEMDisplayUpdate();
	}
	
	CloseRegistryKey(hKey);

	return TRUE;
}

BOOL CDisplayPropPage::GetCurrentColorSetting (DWORD* curColor)
{
	DWORD size = sizeof(DWORD);
	DWORD type = 0;
	HKEY hKey = NULL;

	if (ERROR_SUCCESS == RegOpenKeyEx(HKLM, WBT_MSTSC_SETTINGS, 0, KEY_READ, &hKey))
	{
		if (RegQueryValueEx(hKey, COLOR_SETTINGS, NULL, &type, NULL, &size)
			== ERROR_SUCCESS && type == REG_DWORD )
		{
			RegQueryValueEx(hKey, COLOR_SETTINGS, NULL, NULL, (PBYTE)curColor, &size);

			if (size > 0)
			{
				CloseRegistryKey (hKey);
				DEBUGMSG (1,(L"ATLPROP::GetCurrentColorSetting -- RegistryKeyFound value is %d",*(curColor)));
				return TRUE;
			}
		}

		RegCloseKey(hKey);
	}
	return FALSE;
}



BOOL CDisplayPropPage::UpdateColorChoices()
{
	HKEY hKey = NULL;
	DWORD index = 0;
	DWORD curColor = 0, colorValue = 0;
	TCHAR* szSubKey = NULL;
	TCHAR* colorName = NULL;
	PCOLORINFO curColorInfo = headColorInfo;
	PCOLORINFO newColorInfo=NULL;
	DWORD iCount = -1;
	
	if (!m_bWizard) 
		return TRUE;

	HDC hDC = ::GetDC(NULL);
	UINT screenBpp = 8;
	if (hDC)
	{
		screenBpp = GetDeviceCaps(hDC,BITSPIXEL);
		::ReleaseDC(NULL,hDC);
	}

	if (screenBpp < 8)
		screenBpp = 8;

	hKey = OpenRegistryKey(HKEY_LOCAL_MACHINE,LOCAL_COLOR_KEY,KEY_READ);
	
	if (hKey != NULL )
	{		
		DEBUGMSG(1,(L"ATLPROP::UpdateColorChoices() -- Key opened Succesfully"));

		while (( szSubKey = EnumerateRegistryValues(hKey,index++) ) != NULL )
		{
			if (GetRegistryDWORD(HKEY_LOCAL_MACHINE,LOCAL_COLOR_KEY,szSubKey,&colorValue) != FALSE )
			{
				DEBUGMSG(1,(L"ATLPROP::UpdateColorChoices() -- Enumertaed ValueName is:%s \n",szSubKey));
				DEBUGMSG(1,(L"ATLPROP::UpdateColorChoices() -- Enumertaed value is %d\n",colorValue));
				if (szSubKey != NULL)
				{
					free (szSubKey);
				}

				if (colorValue > screenBpp)
				{
					break;
				}

				if ((szSubKey=EnumerateRegistryValues(hKey,index++)) != NULL)
				{
					colorName = GetRegistryString (HKEY_LOCAL_MACHINE,LOCAL_COLOR_KEY,szSubKey);
					if (colorName)
					{
						DEBUGMSG (1,(L"ATLPROP::UpdateColorChoices() -- Enumerated ValueName is: %s\n",szSubKey));
						DEBUGMSG (1,(L"ATLPROP::UpdateColorChoices() -- Enumerated value is :%s \n",colorName));
						if (szSubKey != NULL)
						{
							free (szSubKey);
						}
						m_colorChoices.SendMessage (CB_ADDSTRING,0,reinterpret_cast<LPARAM>(colorName));
						++iCount;
					}

				}
				else
				{
					DEBUGMSG(1,(L"ATLPROP::UpdateColorChoices() --  Failed to retrieve the Registry Key\n"));
					goto END;
				}
				
				// Create the Struct using the obtained values
				if ((newColorInfo = (PCOLORINFO) malloc (sizeof (COLORINFO))) == NULL)
				{
					RETAILMSG (1,(L"ATLPROP::UpdateColorChoices() -- Memory allocation failed"));
					goto END;
				}
				
				newColorInfo->colorTableValue = colorValue;
				StringCchCopy (newColorInfo->colorTableName, MAX_PATH, colorName);
				newColorInfo->next = NULL;

				if (curColorInfo != NULL)
				{	
					curColorInfo->next = newColorInfo;
					curColorInfo = newColorInfo;
				}
				else
				{
					headColorInfo = newColorInfo;
					curColorInfo  = headColorInfo ;
				}
				if (colorName != NULL)
					free (colorName);
			}
			else
			{
				DEBUGMSG(1,(L"ATLPROP::UpdateColorChoices() -- Failed to retrieve the Registry Key\n"));
				goto END;
			}			
		}
		
			
		if(GetCurrentColorSetting (&curColor))
		{
			//set current color settings as the selected one
			for(curColorInfo = headColorInfo; curColorInfo; curColorInfo=curColorInfo->next) 
			{
				if( curColorInfo->colorTableValue == curColor )
				{
					m_colorChoices.SendMessage(CB_SELECTSTRING,
						0, (LPARAM)curColorInfo->colorTableName);
					break;
				}
			}
		}
		else
		{
			m_colorChoices.SendMessage(CB_SETCURSEL, (WPARAM)iCount, 0); // set current color setting to the first element in the list
			DEBUGMSG (1,(L"ATLPROP::UpdateColorChoices() Selected element is the first element\n"));
		}
	}
	else
	{
		DEBUGMSG(1,(L"ATLPROP::UpdateColorChoices() -- Color Enumeration Keys from the Registry Failed\n"));
		goto END;
	}
	
	CloseRegistryKey(hKey);
	return TRUE;

END: return FALSE;
}


LRESULT CDisplayPropPage::OnDisplayChoicesChange()
{
	m_displayChanged =TRUE;
	
	if (m_displayChoices!=NULL)
	{
		int newSetting = m_displayChoices.SendMessage (CB_GETCURSEL); // get current selection
		if (newSetting != CB_ERR)
		{
			if(!m_bWizard)
				GetOwner().SetReboot (TRUE);
			m_bTested = FALSE;
			
			//============AAS=============
			if ( oemDisplay->TestModeIsSupported() )
			{
				m_test.EnableWindow (newSetting != m_curSetting);
			}
			//============AAS=============
		}
	}
	return 1;
}

LRESULT CDisplayPropPage::OnDisplayTest(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL &bHandled)
{
	HDC hdc = ::GetDC (NULL);
    int height	= GetDeviceCaps (hdc, VERTRES);
    int width	= GetDeviceCaps (hdc, HORZRES);
	::ReleaseDC (NULL, hdc);

	int result = IDCANCEL;
	LPCTSTR pszTitle = LoadStringEx (IDS_TESTING_CAPTION);
	LPCTSTR pszMsg   = LoadStringEx (IDS_DISPLAY_PRETEST);

	if (pszTitle && pszMsg)
		result = MessageBox (pszMsg, pszTitle, MB_ICONINFORMATION | MB_OK);

	
	//This part To be removed when oem code is implemented
	/*
	if(result != IDCANCEL)
	{
		result = MessageBox(LoadStringEx(IDS_TEST_RESOLUTION_MESSAGE),LoadStringEx(IDS_TEST_RESOLUTION_TITLE),MB_OKCANCEL);
	}
	else
	{
		return 0;
	}
	*/
	//End of part to be removed	


	if (result != IDCANCEL)
	{
		CCleanUpDlg cleanUp;
		cleanUp.Create (m_hwnd);
		cleanUp.SetWindowPos (HWND_TOPMOST, 0, 0, width, height, SWP_SHOWWINDOW);

		m_bTested = FALSE;
		TestDesktopResolution();

		//AAS
		OEMDisplayTest();

		cleanUp.SetWindowPos(HWND_TOPMOST, 0, 0, width, height, SWP_HIDEWINDOW);

		pszTitle = LoadStringEx (IDS_TESTING_CAPTION);
		pszMsg   = LoadStringEx (IDS_TEST_SUCCESS);

		result = IDNO;
		if (pszTitle && pszMsg)
			result = MessageBox (pszMsg, pszTitle, MB_ICONINFORMATION | MB_YESNO);

		if (result != IDNO)
			m_bTested = TRUE;
		cleanUp.DestroyWindow();
	}

	
	return 1;	
}


LRESULT CCleanUpDlg::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	// center window
	CenterWindow (GetParent());
	return 1;
}


BOOL CDisplayPropPage::Validate()
{
	return TRUE;
}

BOOL CDisplayPropPage::StoreData()
{
	TCHAR szBuf[10]={0};

	if(!m_bWizard)
	{
	
		m_scrSaverTimeout.GetWindowText(szBuf, sizeof(szBuf) / sizeof(szBuf[0]));

		if ( ( szBuf != NULL ) && ( szBuf[0] != '\0' ) )
			m_nScrTimeout = _ttoi(szBuf);
	}
	return TRUE;
}

BOOL CDisplayPropPage::UpdateDisplayChoices()
{
	HKEY hKey = NULL;
	DWORD index = 0;
	DWORD i = 0;
	DWORD curColor = 0;
	TCHAR* szSubKey = NULL;
	UINT arrayCount = 0;
	LPTSTR curResolution=NULL;
	TCHAR value[s_MAX_DISPLAYVALUELEN ];
	int defaultIndex=-1;
	BOOL fRet = FALSE;
	TCHAR *setting = NULL;

	memset(value ,0, s_MAX_DISPLAYVALUELEN);
	hKey = OpenRegistryKey(HKEY_LOCAL_MACHINE,RESOLUTION_CHOICES_NAME,KEY_READ);
	
	if (hKey != NULL )
	{		
		RETAILMSG(1,(L"ATLPROP::UpdateDisplayChoices1() -- Key opened Succesfully"));
		setting = (TCHAR *)malloc(s_MAX_DISPLAYVALUELEN*sizeof(TCHAR));
		if ( setting  == NULL)
		{
			RETAILMSG(1,(L"ATLPROP::UpdateDisplayChoices1() -- MALLOC to setting FAILED !!!!!\n"));
			goto EXIT;
		}

		if (!GetCurrentDisplaySetting(setting))
		{
			RETAILMSG(1,(L"ATLPROP::UpdateDisplayChoices1() -- GetCurrentDisplaySetting() FAILED !!!!!\n"));
			goto EXIT;
		}

		while( ( szSubKey = EnumerateRegistryValues(hKey,i) ) != NULL )
		{
			
			if((m_displayTableName[index]= GetRegistryString(HKEY_LOCAL_MACHINE,
				RESOLUTION_CHOICES_NAME,
				szSubKey
				)) != FALSE )
			{
				ASSERT(m_displayTableName[index] !=NULL);

				StringCchCopy(value,s_MAX_DISPLAYVALUELEN , m_displayTableName[index] );

				//====================AAS===================
				DWORD Width;
				DWORD Height;
				DWORD colorDepth = 8;
				DWORD Freq;

				_stscanf(value, TEXT("%d x %d @%d"),&Width,&Height,&Freq);

				if ( oemDisplay->ModeIsSupported(Width,Height,colorDepth,Freq) )
				{					
					if ( wcscmp(value,setting)==0 )
					{
						defaultIndex=index;
					}

					m_displayChoices.SendMessage (CB_INSERTSTRING,index,reinterpret_cast<LPARAM>(value));
					RETAILMSG (1,(L"ATLPROP::UpdateDisplayChoices1() the added element is :%s\n",m_displayTableName[index]));
					
					index++;
				}
				//====================AAS===================

				if ( szSubKey != NULL)
				{
					free(szSubKey);
				}
				i++; 
			}
			else
			{
				RETAILMSG(1,(L"ATLPROP::UpdateDisplayChoices1() -- Failed to retrieve the Registry Key\n"));
				//goto EXIT;
				break;
			}			
		}		
	}		
	else
	{
		RETAILMSG(1,(L"ATLPROP::UpdateDisplayChoices1() -- display Enumeration Keys from the Registry Failed\n"));
		goto EXIT;
	}
	if(defaultIndex == -1)
		defaultIndex = 0;
	
	// Please note
	// m_test.EnableWindow
	// Reason for adding this line: After populating the combo box someone need to test the resolution.
	// Reason for removing this line: This will be implemented y OEM and there is no need to eable this button.  
	//m_test.EnableWindow(TRUE);

	m_displayChoices.SendMessage (CB_SETCURSEL,defaultIndex,0);
	//Setting for enabling disabling of test button
	m_curSetting = defaultIndex;

	fRet = TRUE; // Success!

EXIT:
	if (setting)
		free(setting);
	if (hKey)
		CloseRegistryKey(hKey);
	return fRet;
}

BOOL CDisplayPropPage::GetCurrentDisplaySetting (TCHAR *curDisplay)
{
	DWORD size = 0;
	DWORD type = 0;
	HKEY hKey  = NULL;


	if (!curDisplay)
	{
		return FALSE;
	}

	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,LOCAL_COLOR_CONFIG_KEY,0,KEY_READ,&hKey) != ERROR_SUCCESS || hKey == NULL)
	{
		return FALSE;
	}

	if (RegQueryValueEx(hKey,DISPLAY_VALUE_NAME,NULL, &type, NULL, &size) != ERROR_SUCCESS || size == 0)
	{
		CloseRegistryKey(hKey);
		return FALSE;
	}

	if (RegQueryValueEx(hKey,DISPLAY_VALUE_NAME,NULL,NULL,(PBYTE)curDisplay,&size) != ERROR_SUCCESS)
	{
		CloseRegistryKey(hKey);
		return FALSE;
	}

	return TRUE;
}

//
// set the display info in the registry
//
BOOL CDisplayPropPage::SetDisplayInfo( )
{
	UINT selIndex=0;
	UINT colIndex=0 ;
	HKEY hKey = NULL;
	TCHAR szSelDisplay[DISPLAYCOMBO_LEN]={0};

	selIndex = m_displayChoices.SendMessage( CB_GETCURSEL,0,0 );
	
	if ((selIndex == CB_ERR) || (m_displayChoices.SendMessage ( CB_GETLBTEXT,
		(WPARAM)selIndex,
		(LPARAM)szSelDisplay) 
		== CB_ERR ))
	{
		RETAILMSG (1,(L"CDisplayPropPage::SetDisplayInfo( )--CB_GETLBTEXT failed"));
		return FALSE;
	}
		
	hKey = OpenRegistryKey(HKLM,LOCAL_COLOR_CONFIG_KEY,KEY_WRITE);

	if ( hKey  == NULL )
	{
		// Create the Color Key
		hKey = CreateRegistryKey( HKLM,LOCAL_COLOR_CONFIG_KEY );
		if ( hKey == NULL )
		{
			RETAILMSG(1,(L"CDisplayPropPage::SetDisplayInfo() -- Registry key Creation failed"));
			return FALSE;			
		}
	}
	
	for(int k=0; k<MAX_DISPLAY_KEYS; k++)
	{
		if (wcscmp(m_displayTableName[k],szSelDisplay)==0)
		{
			 colIndex= k;
			 break;
		}
	}
	
	// set the color value corresponding to the selection in the combobox.
	SetRegistryKeyString( hKey, DISPLAY_VALUE_NAME , m_displayTableName[colIndex] );
	RETAILMSG (1,(L"CDisplayPropPage::SetDisplayInfo()--The Value Being Stored is: %s & index is : %d",
		m_displayTableName[colIndex],colIndex));
	CloseRegistryKey(hKey);

	//AAS
	OEMDisplayUpdate();
	return TRUE;
}

LRESULT CDisplayPropPage::OnSetActive(int idCtrl, LPNMHDR pnmh, BOOL &bHandled)
{
	if(m_bWizard)
	{

		GetOwner().SetWizFlags (PSWIZB_BACK | PSWIZB_NEXT);
		
	}
	return 0;
}

